/*
 * 版本号存放
 */
function getVersion(sucess) {
	plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
		var version = wgtinfo.version;
		sucess(version);
	});
}

function checkUpdate(isSlience) {
	//isSlience = isSlience ? isSlience : true;
	
	console.log("是否静默...." + isSlience);
	
	checkVision(
		function() {
			hui.loading("正在下载升级包...");
		},
		function(progress) {
			//hui("#progress1").progressBar(progress);
			if(isNull(progress)) {
				hui.loading("正在下载升级包...");
			} else {
				hui.loading("正在下载 ..." + progress + "%");
			}
		},
		function() {
			hui.closeLoading();
		}, isSlience);
}

function checkVision(beginEvent, progressEvent, endEvent, isSlience) {
	// = plus.runtime.getProperty;
	plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
		var version = wgtinfo.version;
		var slience = isSlience ? isSlience : false;
		
		
		
		console.log("开始检查更新...." + slience);

		if(!slience) {
			hui.loading("正在检测新版本...");
		}
		var url = "https://gitee.com/kylexy2009/frqg/raw/master/sensor_update.json" //?appVision=1.0.0.1&appName=com.tenglong.basemanager.dev	

		hui.getJSON(url, function(returnMsg) {
			console.log("更新返回:");
			console.log(JSON.stringify(returnMsg));
			
			if(version != returnMsg.vision) {
				var msg = "发现新版本" + returnMsg.vision + "</br>" + returnMsg.msg;
				hui.confirm(msg, ['下次再说', '升级'], function() {
					downloadFile(returnMsg.url, beginEvent, progressEvent, endEvent);
				},function(){
					localStorage.setItem('upDateTips', true);
				})
			} else {
				if(!slience) hui.toast("已经是最新版本" + version);
			}

			hui.closeLoading();

		}, function() {
			if(!slience) hui.toast("获取最新版本失败,请检查网络!");

			hui.closeLoading();
		})

	});
};

function downloadFile(url, beginEvent, progressEvent, endEvent) {
	var options = {
		method: "GET"
	};
	dtask = plus.downloader.createDownload(url, options, function(task, status) {
		if(status == 200) {} else {
			hui.toast("网络错误,无法下载!");
			endEvent();
		}
	});
	dtask.addEventListener("statechanged", function(task, status) {
		if(!dtask) {
			return;
		}
		switch(task.state) {
			case 1: // 开始
				//huiJsonLog("开始下载...");
				hui.loading();
				break;
			case 2: // 已连接到服务器
				//huiJsonLog("链接到服务器...");
				hui.closeLoading();
				beginEvent();
				break;
			case 3: // 已接收到数据
				////huiJsonLog("下载数据更新:");
				hui.closeLoading();
				var progress = parseInt(parseFloat(task.downloadedSize) / parseFloat(task.totalSize) * 100);
				////huiJsonLog(progress);
				progressEvent(progress);

				break;
			case 4: // 下载完成
				hui.closeLoading();
				//huiJsonLog("下载完成！");
				//huiJsonLog(task.filename);
				installWgt(task.filename);
				endEvent();

				break;
		}
	});

	dtask.start();
};

// 更新应用资源
function installWgt(path) {
	plus.nativeUI.showWaiting("安装升级文件...");
	if(path.indexOf("apk") > 0) {
		plus.nativeUI.closeWaiting();
		plus.runtime.openFile(path);
	} else {
		plus.runtime.install(path, {
			force: true
		}, function() {
			plus.nativeUI.closeWaiting();
			console.log("安装升级文件成功！");
			plus.nativeUI.alert("应用资源更新完成！", function() {
				plus.runtime.restart();
			});
		}, function(e) {
			plus.nativeUI.closeWaiting();
			console.log("更新失败,安装升级文件失败[" + e.code + "]：" + e.message);
			plus.nativeUI.alert("更新失败,安装升级文件失败[" + e.code + "]：" + e.message);
		});
	}
}